/*
 * @Author: esraiskan
 * @Date: 2022-08-04 16:09:19
 * @LastEditTime: 2022-08-04 16:18:33
 * @LastEditors: esraiskan
 * @Description: 描述
 */
/**
 * 写缓存
 * @param key 键
 * @param obj 值对象 内部会转为JSON
 */
 export function write(key, obj) {
  if (window.localStorage) {
      const val = JSON.stringify(obj);
      window.localStorage.setItem(key, val);
  } else {
      throw new Error("Cannot support localStorage.");
  }
}

/**
 * 读缓存
 * @param key 键
 * @param obj 值对象 内部会转为JSON
 */
export function read(key){
  if (window.localStorage) {
      const val = window.localStorage.getItem(key);
      if (val) {
        return JSON.parse(val);
      } else {
        return null;
      }
  } else {
      throw new Error("Cannot support localStorage.");
  }
}

/**
* 删除缓存
* @param key 键
*/
export function remove(key) {
if (window.localStorage) {
  window.localStorage.removeItem(key);
} else {
  throw new Error("Cannot support localStorage.");
}
}